//
//  LXJComposeWeiboToolBar.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/4/5.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit

/// 自定义发微博的工具

/// 定义 toolbar 摁扭的枚举
enum  ComposeToolBarButtonType: Int{
    /// 图库
    case picture = 1
    ///  @某某
    case mention
    /// 话题
    case trend
    /// 表情
    case emoticon
    /// 加号
    case add
}

@available(iOS 9.0, *)
class LXJComposeWeiboToolBar: UIStackView {
    
    // MARK: 属性
    /// 点击摁扭的时候的闭包回调
    var composeToolBarButtonClickClosure: ((ComposeToolBarButtonType) -> ())?
    /// 表情摁扭
    fileprivate lazy var emoticonButton: UIButton = UIButton()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /// 界面加载
        setupUI()
    }
    
    /// 不使用 XIB 加载
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 页面搭建
    fileprivate func setupUI() -> Void {
        /// 水平方向布局
        self.axis = .horizontal
        /// 子控件等比填充
        self.distribution = .fillEqually
        
        /// 添加子控件
        addChildButton(imageName: "compose_camerabutton_background", type: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton =  addChildButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addChildButton(imageName: "compose_add_background", type: .add)
    }
    
    ///  添加子按钮
    @discardableResult
    fileprivate func addChildButton(imageName: String, type: ComposeToolBarButtonType) -> UIButton{
        let button = UIButton()
        /// 设置摁扭的 tag 值
        button.tag = type.rawValue
        ///  设置不同状态下图片
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        /// 监听摁扭的点击事件
        button.addTarget(self, action: #selector(toolBarButtonClickAction(sender:)), for: .touchUpInside)
        /// 禁用摁扭的高亮效果
        button.adjustsImageWhenHighlighted = false
        /// 添加按钮
        addArrangedSubview(button)
        
        return button
    }
    
    // MARK: 摁扭的点击事件
    @objc fileprivate func toolBarButtonClickAction(sender: UIButton) -> Void {
        ///  执行闭包
        composeToolBarButtonClickClosure?(ComposeToolBarButtonType(rawValue: sender.tag)!)
    }
    
    /// 切换表情摁扭的图标样式
    func switchEmoticonBackgroundImage(isEmoticon: Bool) -> Void {
        /// 如果是表情键盘使用系统键盘摁扭
        if isEmoticon {
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
        }else{
            /// 如果是系统键盘使用表情摁扭
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
    }
}
